﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IHISCETeim.Model;
using IHISCETeim.Library;
using IHISCETeim.BLL;
using System.Data;
namespace IHISCETeim.Web.MachineSys.MacModel
{
    public partial class OC55NSLEdit : PageBase
    {
        protected override string DWZRef
        {
            get { return "OCompletingCheckList"; }
        }


        /// <summary>
        /// 需查看的机号
        /// </summary>
        protected string SerialNumber
        {
            get
            {
                return GetRequestValue<string>("SerialNumber");
            }
        }

        protected int Id
        {
            get
            {
                return GetRequestValue<int>("Id",0);
            }
        }
        private OCompletingCheckInfo _obj;
        protected OCompletingCheckInfo obj
        {
            get
            {
                if (_obj == null)
                    _obj = OCompletingCheckController.GetById(Id);
                return _obj;
                //OMenuController.ge
            }
        }

        private SerializableDictionary<string, bool> _QTJDJXMLDict = null;
        /// <summary>
        /// 启动前点检所有的项
        /// </summary>
        protected SerializableDictionary<string, bool> QTJDJXMLDict
        {
            get
            {
                if (_QTJDJXMLDict == null)
                {
                    _QTJDJXMLDict = XmlSerializerHelper.Deserialize<SerializableDictionary<string, bool>>(obj.QDQDJItem);
                }
                return _QTJDJXMLDict;
            }
        }

        private SerializableDictionary<string, XRJCItem> _XRJCDict = null;
        /// <summary>
        /// 性能检查所有的项
        /// </summary>
        protected SerializableDictionary<string, XRJCItem> XRJCDict
        {
            get
            {
                if (_XRJCDict == null)
                {
                    try
                    {
                        _XRJCDict = XmlSerializerHelper.Deserialize<SerializableDictionary<string, XRJCItem>>(obj.XNJCItem);
                       // if (_XRJCDict.Count() <= 0)
                       // {
                           // _XRJCDict = new SerializableDictionary<string, XRJCItem>(CompleteCheckXMLHelper.XRJCDict);
                        //SerializableDictionary<string, float> tempdic = XmlSerializerHelper.Deserialize<SerializableDictionary<string, float>>(CompleteCheckXMLHelper.GetXRJCXMLStr());

                        foreach (var x in CompleteCheckXMLHelper.XRJCDict)
                            {
                                //XRJCItem tempitem = new XRJCItem();
                                //tempitem.Key = x.Key;
                                //tempitem.Value = x.Value.ToString();
                                //tempitem.NGValue = "OK";

                                if (!_XRJCDict.ContainsKey(x.Key))
                                {
                                    XRJCItem tempitem = new XRJCItem();
                                    tempitem.Key = x.Key;
                                    tempitem.Value = "";
                                    tempitem.NGValue = "OK";
                                    _XRJCDict.Add(x.Key, tempitem);
                                }
                               

                               

                                //tempdic[x.Key] = HttpContext.Current.Request.GetRequestValue<float>(x.Key);
                            }
                       // }
                    }
                    catch
                    {
                        /**/

                    }
                }
                return _XRJCDict;
            }
        }


        private SerializableDictionary<string, bool> _GZQRDict = null;
        /// <summary>
        /// 工作确认所有的项
        /// </summary>
        protected SerializableDictionary<string, bool> GZQRDict
        {
            get
            {
                if (_GZQRDict == null)
                {
                    _GZQRDict = XmlSerializerHelper.Deserialize<SerializableDictionary<string, bool>>(obj.GZQRItem);
                    foreach (var x in CompleteCheckXMLHelper.GZQRDict)
                    {
                        if (!_GZQRDict.ContainsKey(x.Key))
                        {
                            _GZQRDict.Add(x.Key,true);
                        }
                    }
                }
                return _GZQRDict;
            }
        }

        protected string GetNGSelect(string controlName)
        {
            string selectHtml = "";//;"<select name=\"NG_铁履H前10m\"><option value=\"OK\" selected>OK</option><option value="NG->OK">NG->OK</option><option value="NG">NG</option></select>";
            string[] ngItme = new string[] { "NG", "NG->OK", "OK" };
            foreach (string item in ngItme)
            {
                string c = XRJCDict[controlName].NGValue == item ? "selected" : "";
                selectHtml += string.Format("<option value=\"{0}\" {1}>{0}</option>", item, c);
            }
            return string.Format("<select name=\"NG_{1}\">{0}</select>", selectHtml, controlName);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (GetRequestValue<string>("ajaxmethod", "") == "edit")
            {
                addmenu();
               
            }
            try
            {
                DataTable dndt= OInspectionController.GetNGItemList(Id, "完成检查书");
                //if (dndt.Rows.Count <= 0)
                //{
                //    DataRow dr = dndt.NewRow();

                //    dndt.Rows.Add(dr);
                //}
                this.rpDJNGItem.DataSource = dndt;
                this.rpDJNGItem.DataBind();
            }
            catch { }
        }

       
        protected virtual void addmenu()
        {
            addmenu("55NSL");
            
            
        }
        protected void addmenu(string MachineType)
        {
           // string X = CompleteCheckXMLHelper.GetGZQRXMLStr();
            OCompletingCheckInfo objedit = Request.SerializableObject<OCompletingCheckInfo>();
            objedit.CCID = Id;
            objedit.QDQDJItem = CompleteCheckXMLHelper.GetQTJDJXMLStr();
            objedit.XNJCItem = CompleteCheckXMLHelper.GetXRJCXMLStr();
            objedit.GZQRItem = CompleteCheckXMLHelper.GetGZQRXMLStr();
            objedit.MachineType = MachineType;
            objedit.CreateDate = obj.CreateDate;

            //2011-12-15 完成检查书问题点
            string[] DJNGExceptionrecords = GetRequestValue<string>("DJNGExceptionrecord", "").Split(new char[] { ',' });
            string[] StationS = GetRequestValue<string>("Station", "").Split(new char[] { ',' });
            string[] SupplierS = GetRequestValue<string>("Supplier", "").Split(new char[] { ',' });
            string[] NGClassS= GetRequestValue<string>("NGClass", "").Split(new char[] { ',' });
            string[] DutyWorkers = GetRequestValue<string>("DutyWorker", "").Split(new char[] { ',' });
            List<ONGInspectionInfo> ngitems = new List<ONGInspectionInfo>();

            for (int index = 0; index < DJNGExceptionrecords.Count(); index++)
            {
                ONGInspectionInfo ngitem = new ONGInspectionInfo();
                if (!string.IsNullOrEmpty(DJNGExceptionrecords[index]))
                {
                    ngitem.Exceptionrecord = DJNGExceptionrecords[index];
                    ngitem.SerialNumber = obj.SerialNumber;
                    ngitem.Station = StationS[index];
                    ngitem.Supplier = SupplierS[index];
                    ngitem.MachineType = obj.MachineType;
                    ngitem.BillDate = obj.CheckDate;
                    ngitem.SourceType = "完成检查书";
                    ngitem.NGClass = NGClassS[index];
                    ngitem.DutyWorker = DutyWorkers[index];
                    ngitems.Add(ngitem);
                }
            }
            //obj.CheckDate=
            this.AddLog(Permission.修改, obj, objedit);
            if (OCompletingCheckController.Update(objedit, ngitems))
                SuccessJson("修改成功", DWZRef);
            else
                ErrorJson("修改失败");


        }
    }
}